import 'package:flutter/material.dart';
import 'package:flutter_module/style/colors.dart';

class CooperationListTopBar extends StatefulWidget
    implements PreferredSizeWidget {
  CooperationListTopBar({Key? key, required this.didSelect}) : super(key: key);

  final Function(int) didSelect;

  @override
  CooperationListTopBarState createState() =>
      CooperationListTopBarState(this.didSelect);

  @override
  Size get preferredSize => Size.fromHeight(60);
}

class CooperationListTopBarState extends State<CooperationListTopBar>
    with TickerProviderStateMixin {
  CooperationListTopBarState(this.didSelect);

  Function(int) didSelect;

  bool showServerObject = false;
  bool showArea = false;

  int currentIndex = 0;
  late AnimationController _serverObjController;
  late Animation<double> _serverObjAnimation;
  late AnimationController _areaController;
  late Animation<double> _areaAnimation;

  @override
  void initState() {
    _serverObjController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 400));
    _serverObjAnimation =
        Tween(begin: .0, end: .5).animate(_serverObjController);
    _areaController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 400));
    _areaAnimation = Tween(begin: .0, end: .5).animate(_areaController);
    super.initState();
  }

  @override
  void dispose() {
    _serverObjController.dispose();
    _areaController.dispose();
    super.dispose();
  }

  reset() {
    setState(() {
      showArea = false;
      showServerObject = false;
      _areaController.reverse();
      _serverObjController.reverse();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Row(
        children: [
          Expanded(
            child: Container(
              color: Colors.white,
              child: MaterialButton(
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  highlightElevation: 0,
                  elevation: 0,
                  child: Text('全部'),
                  onPressed: () {
                    currentIndex = 0;
                    this.didSelect(0);
                    reset();
                  }),
            ),
          ),
          Expanded(
            child: Container(
              color: Colors.white,
              child: MaterialButton(
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  highlightElevation: 0,
                  elevation: 0,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        '服务对象',
                        style: TextStyle(
                            color: showServerObject
                                ? ThemeColors.redEF5D5E
                                : ThemeColors.text333),
                      ),
                      RotationTransition(
                        turns: _serverObjAnimation,
                        alignment: Alignment.center,
                        child: Icon(
                          Icons.expand_more,
                          size: 20,
                          color: showServerObject
                              ? ThemeColors.redEF5D5E
                              : ThemeColors.text333,
                        ),
                      )
                    ],
                  ),
                  onPressed: () {
                    this.didSelect(1);
                    setState(() {
                      showServerObject
                          ? _serverObjController.reverse()
                          : _serverObjController.forward();
                      showServerObject = !showServerObject;
                      showArea = false;
                      _areaController.reverse();
                    });
                  }),
            ),
          ),
          Expanded(
            child: Container(
              color: Colors.white,
              child: MaterialButton(
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  highlightElevation: 0,
                  elevation: 0,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        '地域',
                        style: TextStyle(
                            color: showArea
                                ? ThemeColors.redEF5D5E
                                : ThemeColors.text333),
                      ),
                      RotationTransition(
                        turns: _areaAnimation,
                        alignment: Alignment.center,
                        child: Icon(
                          Icons.expand_more,
                          size: 20,
                          color: showArea
                              ? ThemeColors.redEF5D5E
                              : ThemeColors.text333,
                        ),
                      )
                    ],
                  ),
                  onPressed: () {
                    this.didSelect(2);
                    setState(() {
                      showArea
                          ? _areaController.reverse()
                          : _areaController.forward();
                      showArea = !showArea;
                      showServerObject = false;
                      _serverObjController.reverse();
                    });
                  }),
            ),
          ),
        ],
      ),
    );
  }
}
